﻿using Microsoft.EntityFrameworkCore;
using MyMes.Infrastructure;
using MyMES.IServices.Testingitemes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Utilities.Response;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.Core;

namespace MyMES.Services.Testingitemes
{
    public class TestingitemsServices : ITestingitemsServices
    {
        private readonly IBaseRepository<testingitems> _testingitemsRepository;

        public TestingitemsServices(IBaseRepository<testingitems> testingitemsRepository)
        {
            _testingitemsRepository = testingitemsRepository;
        }

        public async Task<WebResponseContent> AddTestingitemsModel(testingitems Testingitems)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _testingitemsRepository.AddAsync(Testingitems);
                return response.OK("添加检测项成功");
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<WebResponseContent> DelTestingitemsModel(testingitems Testingitems)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {

                await _testingitemsRepository.Remove(Testingitems);
                return response.OK("删除检测项成功");
            }
            catch (Exception ex)
            {
                return response.Error($"删除检测项失败：{ex.Message}");
            }
        }

        public async Task<PageGridData<testingitems>> GetTestingitems(string? PlanNo, int pageIndex = 1, int pageSize = 10)
        {
            var pageData = new PageGridData<testingitems>();
            var query = _testingitemsRepository.GetAll(x=>x.PlanNo==PlanNo);

            pageData.total = query.Count();

            pageData.rows = await query.OrderByDescending(x => x.id)
                             .Skip((pageIndex - 1) * pageSize).Take(pageSize)
                                  .ToListAsync();
            return pageData;
        }

        public async Task<WebResponseContent> UpdateTestingitemsModel(testingitems Testingitems)
        {
            WebResponseContent response = new WebResponseContent();
            try
            {
                await _testingitemsRepository.UpdateAsync(Testingitems);
                return response.OK("更新检测项成功");
            }
            catch (Exception ex)
            {
                return response.Error($"更新检测项失败：{ex.Message}");
            }
        }
    }
}
